package org.example;/*
 * title:
 * author:
 * date:
 * description:
 * */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileLockSimulator {
    private static final String SOURCE_FILE_PATH = "C:\\Users\\11924\\Desktop\\删除用的.txt"; // 假设的源文件路径
    private static final String DESTINATION_FILE_PATH = "C:\\Users\\11924\\Desktop\\destination.txt"; // 目标文件路径

    public static void main(String[] args) throws IOException, InterruptedException {
        // 主线程尝试删除文件，模拟最终需要释放资源的情景
        Thread deleteThread = new Thread(() -> {
            System.out.println("Delete thread starts.");
            try {
                Thread.sleep(2000); // 等待一段时间，模拟其他操作
                deleteFile(DESTINATION_FILE_PATH); // 尝试删除文件
            } catch (InterruptedException | IOException e) {
                e.printStackTrace();
            }
            System.out.println("Delete thread ends.");
        });
        deleteThread.start();

        // 子线程启动，内部创建孙子线程进行文件操作
        Thread subThread = new Thread(() -> {
            System.out.println("Sub-thread starts.");
            copyFileWithSubThread();
            System.out.println("Sub-thread ends.");
        });
        subThread.start();

        subThread.join(); // 确保子线程执行完毕
        deleteThread.join(); // 确保删除线程也执行完毕
        System.out.println("All threads have finished.");
    }

    private static void copyFileWithSubThread() {
        Thread grandChildThread = new Thread(() -> {
            System.out.println("GrandChild-thread starts file operation.");
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream(SOURCE_FILE_PATH);
                fos = new FileOutputStream(DESTINATION_FILE_PATH);

                // 文件操作，这里简化为读取并立即写入，模拟持有文件资源
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = fis.read(buffer)) != -1) {
                    fos.write(buffer, 0, bytesRead);
                    Thread.sleep(60000);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("GrandChild-thread finished file operation.");
        }, "GrandChildThread");
        grandChildThread.start();
        try {
            grandChildThread.join(); // 等待孙子线程完成文件操作
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    private static void deleteFile(String filePath) throws IOException {
        try {
            boolean deleted = new java.io.File(filePath).delete();
            if (deleted) {
                System.out.println("File deleted successfully.");
            } else {
                System.out.println("Failed to delete the file.");
            }
        } catch (SecurityException se) {
            System.err.println("SecurityException occurred while deleting the file: " + se.getMessage());
        }
    }
}
